<?php

namespace Liuhelong\LaravelAdmin\Wechat\Actions\WechatOffiaccountMaterial;

use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Request as R;

class Preview extends RowAction
{
    public $name = '预览';
    
    public function form()
    {
		$this->text('wechat_account', '微信号')->required();
		$this->hidden('type')->value(R::get('type'));
    }
	
	public function handle(Model $model, Request $request)
    {
		$wechat = app('liuhelong_wechat_service');
		$type = ucfirst($request->type);
		try{
			if(!$wechat->handle('broadcasting','preview'.$type.'ByName',[$model->getKey(), $request->wechat_account])){
				throw new \Exception('发送预览失败！');
			}
					
			return $this->response()->success('发送预览成功')->refresh();
		}catch(\Exception $e){
			return $this->response()->error($e->getMessage())->refresh();
		}
    }
}